/**
 * 
 */
$(function() {
	// 初始化UI组件
	initUI();
	// 绑定事件
	event();
});

function initUI() {
	// 初始化accordion
	$('#accordion').accordion({
		fit : true,
		border : false
	});

	var menu = [
			{
				title : '考勤导入',
				iconCls:'icon-search',
				content : '<ul><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">手动导入</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">自动导入</span></div></li></ul>',
				selected : false
			},
			{
				title : '考勤核对',
				iconCls:'icon-search',
				content : '<ul><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">考勤补签</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">迟到早退</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">出差上报</span></div></li></ul>',
				selected : false
			},
			{
				title : '考勤导出',
				iconCls:'icon-search',
				content : '<ul><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">导出报表</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="static/html/person.html">自动汇报</span></div></li></ul>',
				selected : false
			},
			{
				title : '系统管理',
				iconCls:'icon-search',
				content : '<ul><li><div><span class="amsicon amsicon-nav"></span><span data-url="user/userAdmin.action">人员管理</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="org/orgAdmin.action">机构管理</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="role/roleAdmin.action">角色管理</span></div></li><li><div><span class="amsicon amsicon-nav"></span><span data-url="rights/rightsAdmin.action">权限管理</span></div></li></ul>',
				selected : true
			} ];
	
	var menus;
//	$.ajax({
//		url:'login/getMenu.action',
//		async:false,
//		success:function(rightsList){
//			$.each(rightsList,function(i,rights){
//				menus[i].title = rights.name;
//				menus[i].iconCls = rights.icon;
//				menus[i].content = rights.icon;
//			})
//			
//		}
//	});
	
	
	console.log(menus);
	
	$.each(menu, function(key, value) {
		$('#accordion').accordion('add', value);
	});

	// 初始化tabs
	$('#tabs').tabs({
		fit : true,
		border : false
	});

	var welcomPage = 'login/welcome.action';

	addTab('欢迎使用',welcomPage);
}

function event() {
	// 为导航菜单的元素绑定打开tab事件
	$('li').click(function() {
		var tabTitle = $(this).find("span").filter("span[data-url]").text();
		// 获取<span data-url="static/html/org.html">中data-url的值
		var url = $(this).find("span").filter("span[data-url]").data('url');
		addTab(tabTitle, url);
		$('li div').removeClass("selected");
		$(this).children().addClass("selected");
	});

	$('li').hover(function() {
		$(this).children().addClass("hover");
	}, function() {
		$(this).children().removeClass("hover");
	});

}

// 添加tab
function addTab(tabTitle, url) {
	if ($('#tabs').tabs('exists', tabTitle)) {
		$('#tabs').tabs('select', tabTitle)
	} else {
		$('#tabs').tabs('add', {
			title : tabTitle,
			content : createFrame(url),
			selected : true,
			closable : true
		});
	}
}

function createFrame(url)
{
	var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}
